package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.mapper.TActivityRemarkMapper;
import com.bjpowernode.model.TActivityRemark;
import com.bjpowernode.model.TUser;
import com.bjpowernode.query.ActivityRemarkQuery;
import com.bjpowernode.service.ActivityRemarkService;
import com.bjpowernode.util.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private TActivityRemarkMapper tActivityRemarkMapper;

    /**
     * 提交保存活动备注
     *
     * @param activityRemarkQuery
     * @return
     */
    @Override
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery) {

        TActivityRemark tActivityRemark = new TActivityRemark();

        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);

        tActivityRemark.setCreateTime(new Date());//创建时间

        //设置一下创建人
        Integer loginUserId = JWTUtil.parseUserFromJWT(activityRemarkQuery.getJwt()).getId();
        tActivityRemark.setCreateBy(loginUserId);

        return tActivityRemarkMapper.insertSelective(tActivityRemark);
    }

    @Override
    public PageInfo<TActivityRemark> getActivityRemarkByPage(Integer current, Integer activityId) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //2.查询
        List<TActivityRemark> list = tActivityRemarkMapper.selectActivityRemarkPage(activityId);
        //3.封装分页数据到PageInfo
        PageInfo<TActivityRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TActivityRemark getActivityRemarkById(Integer id) {
        return tActivityRemarkMapper.selectByPrimaryKey(id);
    }

    /**
     * 编辑市场活动备注（提交保存）
     *
     * @param activityRemarkQuery
     * @return
     */
    @Override
    public int updateActivityRemark(ActivityRemarkQuery activityRemarkQuery) {
        TActivityRemark tActivityRemark = new TActivityRemark();

        //需要把UserQuery数据复制到TUser对象中
        //spring提供了一个工具类：BeanUtils，它可以实现把一个对象的属性值复制到另一个对象中
        //复制的要求：源对象和目标对象，两个对象的成员变量名称相同，成员变量的类型也相同，这样才可以复制
        BeanUtils.copyProperties(activityRemarkQuery, tActivityRemark);


        //设置一下编辑时间
        tActivityRemark.setEditTime(new Date());

        //设置一下编辑人
        Integer loginUserId = JWTUtil.parseUserFromJWT(activityRemarkQuery.getJwt()).getId();
        tActivityRemark.setEditBy(loginUserId);

        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }

    @Override
    public int delActivityRemarkById(Integer id) {
        //逻辑删除：就是修改一下数据的状态，数据本身并没有从数据库中删除；
        //物理删除：就是真的把数据删除，数据库中就没有这条数据了
        TActivityRemark tActivityRemark = new TActivityRemark();
        tActivityRemark.setId(id);
        tActivityRemark.setDeleted(1); //删除状态（null或者0正常，1删除）
        return tActivityRemarkMapper.updateByPrimaryKeySelective(tActivityRemark);
    }
}
